package g.beny.framework;

import java.util.HashMap;

import g.beny.framework.constant.Constant;
import g.beny.framework.util.MD5;
import g.beny.framework.web.ActionSupport;
import g.beny.framework.web.Result;
import g.beny.manage.bean.Accountuser;

import org.nutz.conf.NutConf;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

@Filters
public class AuthAction extends ActionSupport {

	/**
	 * 登录认证
	 * 
	 * @methodName: auth
	 * @Description:
	 * @CreatedOn: 2012-8-17 下午10:11:28
	 * @author gson
	 *         <p>
	 * @return
	 */
	@At("/auth")
	@POST
	public Result auth(@Param("uname") String uname,
						@Param("upwd") String upwd, 
						@Param("code") String code,
						@Param("type") String type,
						@Param("remember") Boolean remember) {
		@SuppressWarnings("unchecked")
		HashMap<String,String> sysAccount = (HashMap<String,String>) NutConf.get(Constant.Config.SYSTEM_ACCOUNT);
		if(uname.trim().toLowerCase().equals(sysAccount.get("accountNumber"))){
			if(!getSesAttr(Constant.Config.VERIFICATION_CODE_KEY).equals(code.trim().toLowerCase())){
				return new Result(false, "验证码错误！");
			}
			if(!MD5.encrypt(upwd).equals(sysAccount.get("password"))){
				return new Result(false, "密码错误！");
			}
			Accountuser ac = new Accountuser();
			ac.setAccountNumber(sysAccount.get("accountNumber"));
			ac.setUserId("-1");
			setSesAttr(Constant.Config.MGR_USER_SESSION_KEY, ac);
			return new Result("认证成功！");
		}else{
			return new Result(false,"不允许账户！");
		}
	}

	/**
	 * 注销
	 * 
	 * @methodName: logoff
	 * @Description:
	 * @CreatedOn: 2012-8-17 下午10:11:40
	 * @author gson
	 *         <p>
	 * @return
	 */
	@At("/logoff")
	public Result logoff() {
		getSession().invalidate();
		return new Result();
	}

	/**
	 * 退出
	 * 
	 * @methodName: exit
	 * @Description:
	 * @CreatedOn: 2012-8-17 下午10:11:54
	 * @author gson
	 *         <p>
	 * @return
	 */
	@At("/exit")
	public Result exit() {
		getSession().invalidate();
		return new Result();
	}
}
